package users

import (
	"context"
	"product/common/validator"

	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CheckUsernameExistLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewCheckUsernameExistLogic Check valid username
func NewCheckUsernameExistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckUsernameExistLogic {
	return &CheckUsernameExistLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CheckUsernameExistLogic) CheckUsernameExist(req *types.CheckUsernameExistReq) (resp *types.CheckUsernameExistResp, err error) {
	if validator.IsEmptyString(req.UserName) {
		return &types.CheckUsernameExistResp{Message: "用户名不能为空",
			Ok: false}, nil
	}
	exist, err := l.svcCtx.UsersModel.CheckUserNameExists(l.ctx, req.UserName)
	if err == nil && exist {
		return &types.CheckUsernameExistResp{
			Message: "用户名已存在",
			Ok:      false,
		}, nil
	}

	return &types.CheckUsernameExistResp{
		Message: "用户名可用",
		Ok:      true,
	}, nil
}
